
    /********************************************************/
    /*                                                      */
    /*                                                      */
    /*         Copyright (C), 1993  SEIKO EPSON Corp.       */
    /*         ALL RIGHTS RESERVED                          */
    /*                                                      */
    /*                                                      */
    /*  file name : div.c                                   */
    /*                                                      */
    /*  Revision history                                    */
    /*      93/12/17    first release       S.Akaiwa        */
    /*      97/2/20	modified	    T.Katahira	    */
    /*			change function declaration	    */
    /*					to the ANSI style   */
    /*                                                      */
    /********************************************************/

#ifdef      UNIX
#include    "stdlib.h"
#include    "errno.h"
#else
#include    <stdlib.h>
#include    <errno.h>
#endif

div_t
div(int iNum, int iDen)

    /********************************************************/
    /*                                                      */
    /*  The div function calculate the quotient and         */
    /*  remainder of the division of the integer numerator  */
    /*  iNum by the integer denominator iDen.               */
    /*  If 0 is specified by iDen, quotient and remainder   */
    /*  are set to 0 and errno is set to EDOM.              */
    /*                                                      */
    /*      RETURN  The quotient and remainder in a         */
    /*              structure                               */
    /*                                                      */  
    /********************************************************/

    /* int             iNum    ;                  numerator */
    /* int             iDen    ;                denominator */
    {

     int            iAbsNum ;         /* absolute numerator */
     int            iAbsDen ;       /* absolute denominator */
     div_t          stAns   ;           /* result structure */
     
     if(iDen == 0){                    /* check denominator */
        errno = EDOM;                     
        stAns.quot = 0;
        stAns.rem = 0;
        return        stAns;              /* divide by zero */
     }

     iAbsNum = abs(iNum);         /* get absolute numerator */
     iAbsDen = abs(iDen);       /* get absolute denominator */

     stAns.quot = iAbsNum / iAbsDen;  /* calculate quotient */
     stAns.rem = iAbsNum % iAbsDen;  /* calculate remainder */
     
     if(((iNum < 0) && (iDen > 0)) || ((iDen < 0) && (iNum > 0))){
        stAns.quot = -stAns.quot;   /* get sign of quotient */
     }
     
     if(iNum < 0){
        stAns.rem = -stAns.rem;    /* get sign of remainder */
     }

     return        stAns;                  /* return result */
    }

